<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;角色管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;添加角色</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">角色添加</el-divider>
        <el-form
            :model="roleForm"
            :rules="rules"
            ref="roleForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-form-item label="角色名称:" prop="name">
            <el-input v-model="roleForm.name" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色简介:" prop="description">
            <el-input v-model="roleForm.description" placeholder="请输入角色简介"></el-input>
          </el-form-item>
          <el-form-item label="角色序号:" prop="sort">
            <el-input v-model="roleForm.sort" placeholder="请输入角色序号"></el-input>
          </el-form-item>
          <el-form-item label="角色权限:" prop="permissionIds">
            <el-select v-model="roleForm.permissionIds"  multiple placeholder="请选择角色权限">
              <el-option
                  v-for="item in roleListOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-divider content-position="left">确认角色添加</el-divider>
          <el-form-item>
            <el-button type="primary" @click="submitForm('roleForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('roleForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleListOptions:[],
      roleForm: {
        name: '',
        description: '',
        sort: '',
        permissionIds: []
      },
      rules: {
        name: [
          {required: true, message: "请输入角色名称", trigger: 'blur'},
          {pattern: /([\u4E00-\u9FA5])+/, message: "请输入中文", trigger: 'blur'}
        ],
        description: [
          {required: true, message: "请输入角色简介", trigger: 'blur'},
          {pattern: /([\u4E00-\u9FA5])+([\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c])*/, message: "请输入中文", trigger: 'blur'}
        ],
        sort: [
          {required: true, message: "请输入角色序号", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        permissionIds: [
          {required: true, message: "请选择角色权限", trigger: 'change'},
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9090/roles/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.roleForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加角色成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadRoleList() {
      let url = 'http://localhost:9090/permissions';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.roleListOptions = responseBody.data;
      });
    }
  },
  mounted() {
    this.loadRoleList();
  }
}

</script>


<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}
</style>